package com.clstu.qqclient.view;

import com.clstu.qqclient.service.UserClientService;
import com.clstu.qqclient.utilies.Utility;

import java.util.Scanner;

/**
 * QQ的菜单界面(这是一个没有窗口的聊天程序,也可以使用全部功能)
 */
public class QQView {
    private static final Scanner scanner = new Scanner(System.in);
    private boolean loop = true;//(控制是否退出系统)
    private final UserClientService userClientService = new UserClientService();//用于判断是否登录成功的成员

    public void mainMenu(){
        while (loop){
            System.out.println("========欢迎来到登录网络通信系统===========");
            System.out.println("\t\t1.用户登录");
            System.out.println("\t\t9.退出系统");
            System.out.print("    请输入:");
            //接受用户输入
            String key = Utility.readString(1);
            switch (key){
                case "1":
                    System.out.print("请输入用户名:");
                    String uerID = Utility.readString(50);
                    System.out.print("请输入密码:");
                    String pwd = Utility.readString(50);

                    //检查是否登录成功
                    if(userClientService.checkUser(uerID,pwd)){
                        System.out.println("=========欢迎用户 ( "+uerID+" )==============");
                        while (loop){
                            System.out.println("=========网络通信系统二级菜单 用户( "+uerID+" )========");
                            System.out.println("\t\t1.显示用户列表");
                            System.out.println("\t\t2.群发消息");
                            System.out.println("\t\t3.私聊消息");
                            System.out.println("\t\t4.发送文件");
                            System.out.println("\t\t9.退出系统");
                            System.out.print("  请输入你的选择:");
                            key = Utility.readString(1);
                            switch (key){
                                case "1"://获取在线用户列表
                                    userClientService.getOnline(uerID);
                                    break;
                                case "2"://群发消息
                                    userClientService.sentAll(uerID);
                                    break;
                                case "3"://私聊
                                    userClientService.sentMessage(uerID);
                                    break;
                                case "4"://发文件
                                    userClientService.sentFile(uerID);
                                    break;
                                case "9":
                                    userClientService.logout(uerID);
                                    System.exit(0);//表示进程退出,它所触发的所有子线程也会一并退出,程序正常退出结束
                            }
                            pause();
                        }
                    }else {
                        System.out.println("========登录失败==========");
                        pause();
                    }
                    break;
                case "9":
                    System.out.println("您已经退出系统,感谢您的使用");
                    loop = false;
                    break;
            }

        }
    }

    public static void pause(){//暂停一下,
        System.out.print("输入回车继续......");
        scanner.nextLine();
    }

}
